package com.dajingling.wx.Controller;

import com.dajingling.bean.EUserExpress;
import com.dajingling.bean.Message;
import com.dajingling.bean.UserAdmin;
import com.dajingling.mvc.ResponseBody;
import com.dajingling.service.EUserExpressService;
import com.dajingling.service.UserAdminService;
import com.dajingling.util.JSONUtil;
import com.dajingling.util.RandomUtil;
import com.dajingling.util.SMSUtil;
import com.dajingling.util.UserUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserAdminController {
    @ResponseBody("/wxloginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String code = RandomUtil.getCode()+"";
        boolean flag = SMSUtil.loginSend(userPhone, code);
        //String userPhone = "15177228631";
        //String code = "123456";
        //boolean flag = true;
        Message msg = new Message();
        if(flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送,请查收!");
        }else{
            //短信发送失败
            msg.setStatus(1);
            msg.setResult("验证码下发失败,请检查手机号或稍后再试");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(msg);
        System.out.println("微信登录发送验证码："+json);
        return json;
    }
    @ResponseBody("/wxlogin.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();
        if(sysCode == null){
            //这个手机号未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        }else if(sysCode.equals(userCode)){
            //这里手机号码和短信一致 , 登陆成功
            UserAdmin user = new UserAdmin();
            if(EUserExpressService.findByUserPhone(userPhone)!=null){
                //快递员
                System.out.println("快递员已登录！");
                msg.setStatus(1);
                user.setUser(false);
                UserUtil.setUserPhone(request.getSession(), userPhone);
            }else if(UserAdminService.findByUserPhone(userPhone)!=null){
                //用户
                System.out.println("用户已登录！");
                msg.setStatus(0);
                user.setUser(true);
                UserUtil.setUserPhone(request.getSession(), userPhone);
            }else {
                System.out.println("未登录过，完成注册！");
                user.setUserName("未填写");
                user.setUserPhone(userPhone);
                user.setIdCard("未填写");
                UserAdminService.insert(user);
            }
            user.setUserPhone(userPhone);
            UserUtil.setWxUser(request.getSession(),user);
        }else{
            //这里是验证码不一致 , 登陆失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("微信登录："+json);
        return json;
    }

    @ResponseBody("/wxloginpwd.do")
    public String loginPwd(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String password = request.getParameter("password");
        EUserExpress e1 = EUserExpressService.findByUserPhone(userPhone);
        UserAdmin e2 = UserAdminService.findByUserPhone(userPhone);
        Message msg = new Message();
        UserAdmin user = new UserAdmin();
        if(e1 != null){
            if (e1.getPassword().equals(password)){
                System.out.println("快递员已登录！");
                Cookie cookie1 = new Cookie("userPhone_cookie",userPhone);
                response.addCookie(cookie1);
                Cookie cookie2 = new Cookie("password_cookie",password);
                response.addCookie(cookie2);
                msg.setStatus(1);
                user.setUser(false);
                user.setUserPhone(userPhone);
                UserUtil.setUserPhone(request.getSession(), userPhone);
                UserUtil.setWxUser(request.getSession(),user);
            }else {
                msg.setStatus(-1);
                msg.setResult("密码错误！");
            }
        }else if(e2 != null){
            if(e2.getPassword().equals(password)){
                System.out.println("用户已登录！");
                Cookie cookie1 = new Cookie("userPhone_cookie",userPhone);
                response.addCookie(cookie1);
                Cookie cookie2 = new Cookie("password_cookie",password);
                response.addCookie(cookie2);
                msg.setStatus(0);
                user.setUser(true);
                user.setUserPhone(userPhone);
                UserUtil.setUserPhone(request.getSession(), userPhone);
                UserUtil.setWxUser(request.getSession(),user);
            }else {
                msg.setStatus(-1);
                msg.setResult("密码错误！");
            }
        }else{
            msg.setStatus(-2);
            msg.setResult("用户名不存在！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("微信登录："+json);
        return json;
    }

    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        UserAdmin user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if(isUser) {
            msg.setStatus(0);
        }else {
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        System.out.println("判断是否用户："+json);
        return json;
    }


    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //1.    销毁session
        request.getSession().invalidate();
        //2.    给客户端回复消息
        Message msg = new Message();
        String json = JSONUtil.toJSON(msg);
        System.out.println("退出登录："+json);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String userUpdate(HttpServletRequest request, HttpServletResponse response) {
        String oldUserPhone = UserUtil.getWxUser(request.getSession()).getUserPhone();
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String idCard = UserAdminService.findByUserPhone(oldUserPhone).getIdCard();
        String password = request.getParameter("password");
        UserAdmin e = new UserAdmin(userName, password, userPhone, idCard);
        boolean flag = UserAdminService.update(oldUserPhone, e);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("修改用户：" + json);
        return json;
    }

    @ResponseBody("/express_user/update.do")
    public String expressUpdate(HttpServletRequest request, HttpServletResponse response){
        String oldUserPhone = UserUtil.getWxUser(request.getSession()).getUserPhone();
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String idCard = EUserExpressService.findByUserPhone(oldUserPhone).getIdCard();
        String password = request.getParameter("password");
        EUserExpress e = new EUserExpress(userName,password,userPhone,idCard);
        System.out.println(e);
        boolean flag = EUserExpressService.update(oldUserPhone,e);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("修改快递员："+json);
        return json;
    }
}